<?php
/*
*********************************************************************
**  gCache.                                                         *
**  Cesar D. Rodas <saddor@cesarodas.com>                           *
**                                                                  *
*********************************************************************
**  The author disclaims the copyright of this class. You are       *
**  legaly free to use, modify and redistribute the class.          *
**  Helps, bugs reports, and contributions could be done at         *
**  saddor@cesarodas.com. Also this email is a GTalk account, and   *
**  I can be contacted throught this way too.                       *
*********************************************************************
**  gCache 2.0                                                      *
**                                                                  *
**  The major differece from gCache an gCache 2.0 is that gCache 2  *
**  gives the posibility to store variables that usually changes    *
**  for examples, visitors count in real time, votes from users     *
**  and others changes that usually will require the cache rebuild. *
**                                                                  *
**  The variable can take any value "on the fly" typicaly the values* 
**  are taken from database, but that is a wrong think because      *
**  gCache was designed to avoid database access, so there is a new *
**  class gVars that implements the gCache but allowing to store    *
**  any kind of variables (scalar, vectors, and objects)            *
**                                                                  *
**  For major performance, the cache generated by gCache 2.0 is not *
**  flat files as the older versions does, it write PHP scripts     *
**  with the content and calls to the vars.                         *
**  This feature increment the variable replacement to the best     *
**  because parsing, replacing and other is doing by PHP intreperter*
**                                                                  *
**  gCache 2.0 open gCache 1.0 cached-files without any problem but *
**  gCache 1.0 will have problems to display cached-files generated *
**  by gCache with variables                                        *
*********************************************************************
*/
class gCache extends SafeIO {
    var $folder;
    var $contentId;
    var $content;
    var $timeout;
    var $cacheValid;
    var $fileCache;
    
    /*
    **  Class constructor
    */
    function gCache() {
        global $HTTP_SERVER_VARS;
        $this->userSupportCompression = strpos($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'], 'gzip') !== false;
    }
    
    
    function Valid() {
        $path = $this->GetCacheRealPath();
        if ( $this->open($path,READ) === false) {
            $this->cacheError();
            exit();
        }

        $this->cacheValid=false;
        if ($this->stat['size'] > 0) {
            if ($this->timeout == 0 || ($this->stat['mtime']+$this->timeout*60) > time() ) {
                $this->cacheValid=true;
                $this->content = $this->read( $this->stat['size'] );
                $this->fileCache = $path;
            }
            $this->close();
        }
        return $this->cacheValid;
    }
    
    /*
    **  Begins the Cache Area
    */
    function capture() {
        ob_start( array($this,'cacheWrite') );
    }

    /*
    ** Ends the Cache Area
    */
    function endcapture() {
        ob_end_flush();
    }
    
    function cacheWrite($content) {
          /*
         *    Try to open the cache as exclusive for write a new content. If the
         *    server is too busy(many readers) for lock, so do not cache, but always shows the content.
         */
        if ( $this->open( $this->GetCacheRealPath(), WRITE )  === true) {
            $this->write( $content, strlen($content)) ; 
            $this->close();
        }
        return $content;
    }
    
    /*
     *
     * 
     */
    function cacheError() {
        echo "Internal Server Error, Please try your request in 5 seconds";
        
    }
    
    
    /*
    **  Gets the real name of the cache file
    */
    function GetCacheRealPath() {
        return $this->folder."/".$this->contentId;
    }
    
}

?>